********************************************************************************
clear all
set more off
capture log close

********************************************************************************
** File paths
** Set the current directory to the same folder where the code is

global input "../input" 
global output "../output" 
global temp "../temp"

********************************************************************************
* Start log, save it into temp folder
log using "$temp/regressions_panel", replace

********************************************************************************
** Coefficient plot for panel
********************************************************************************
program pCoefficientPlot
{
matrix vB = _b[vAfterTreatmentM3]
matrix vSE = _se[vAfterTreatmentM3]

foreach i in 2 1 {
	matrix vB = vB \ _b[vAfterTreatmentM`i']
	matrix vSE = vSE \ _se[vAfterTreatmentM`i']
}

matrix vB = vB \ 0
matrix vSE = vSE \ 0

forvalues i = 1/4 {
	matrix vB = vB \ _b[vAfterTreatment`i']
	matrix vSE = vSE \ _se[vAfterTreatment`i']
}

matrix vB = vB'
matrix vSE = vSE'
matrix list vB
matrix list vSE

coefplot matrix(vB), se(vSE) vertical yline(0) levels(90) xtitle("Years from treatment", size(large)) scheme(s1mono) mcolor(navy) ciopts(recast(rcap) lcolor(navy)) xlabel(,labsize(large)) ylabel(,labsize(large)) xlabel(1 "-3" 3 "-1" 5 "1" 7 "3" , valuelabel) xline(4, lpattern(dash)) 
}
end

********************************************************************************
use $temp/pagelength_users_edits, clear

keep if vLanguage!="nl"
tabulate vLanguage 
tabulate vMonth

xtset vNumPageID vMonth

********************************************************************************
gen vAfterTreatmentM1  = (vMonth>tm(2012m8) & vMonth<=tm(2013m8))*vTreatmentGroup
gen vAfterTreatmentM2  = (vMonth>tm(2011m8) & vMonth<=tm(2012m8))*vTreatmentGroup
gen vAfterTreatmentM3  = (vMonth<=tm(2011m8))*vTreatmentGroup
tabulate vMonth vAfterTreatmentM1

label variable vAfterTreatment1 "Treatment $\times$ year 1"
label variable vAfterTreatment2 "Treatment $\times$ year 2"
label variable vAfterTreatment3 "Treatment $\times$ year 3"
label variable vAfterTreatment4 "Treatment $\times$ year 4"

********************************************************************************
** Page length is measured in the beginning of the month, here want to bring it a month forward, so that page length increase in August would be measured by page length in the beginning of September; also we are excluding Aug 2014 and that corresponds to the page length in the beginning of Sept 2014. 
gen vFLogLengthMinusTreatment = F1.vLogLengthMinusTreatment
tabstat vFLogLengthMinusTreatment, by(vMonth) stat(n)

********************************************************************************
** Panel
********************************************************************************
** Balanced panel
foreach var of varlist vFLogLengthMinusTreatment vUsers vEditDays vEditsNotOurText vEditDist vEditDistDailyCap {
xtreg `var' vAfterTreatment1 vAfterTreatment2 vAfterTreatment3 vAfterTreatment4 vAfterTreatmentM* i.vMonth if vMonth!=tm(2014m8) & vBalancedPanel == 1  & vMonth>tm(2010m8), fe cluster(vNumPageID) 
estimates store `var'
estadd local lFE "Yes"
sum `var'  if vMonth!=tm(2014m8) & vBalancedPanel == 1 & vMonth>tm(2010m8)
estadd scalar p_mean = `r(mean)'
estadd scalar p_sd = `r(sd)'
** Graph
pCoefficientPlot
graph export $output/gRegrPanel_`var'.pdf, replace
}

estout vFLogLengthMinusTreatment vUsers vEditDays vEditsNotOurText vEditDist vEditDistDailyCap using $output/tRegrPanel.tex, keep( vAfterTreatment1 vAfterTreatment2 vAfterTreatment3 vAfterTreatment4) cells(b(star fmt(3)) se(par fmt(3))) style(tex) mlabels(none) collabels(none) label starlevels(* .1 ** .05 *** .01) stats(lFE p_mean p_sd N , fmt(3 3 3 %9.0g ) labels("Language-City FE" "Mean dep. var." "SD dep. var." "Observations")) indicate("Month FE = *vMonth*" "Treatment $\times\{$ year-1,-2,-3 $\}$ = vAfterTreatmentM1")  prehead(`" & Log. length & \# users & \# edits & \# edits & Edit & Capped \\ & excluding & & & excluding & distance &  edit\\  & treatment & & &  treatment &  &  distance\\& (1) & (2) & (3) & (4) & (5) & (6)\\ \hline "') replace

********************************************************************************
log close
clear
